<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">招聘信息</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!recruit">首页</a>
          <a><cite>招聘信息</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="recruitment-search-key">
                <option value="">-请选择-</option>
                <option value="companyname">公司名字</option>
                <option value="skills">技术</option>
                <option value="employeeCount">招聘人数</option>
                <option value="experience">工作年限</option>

            </select>&emsp;
            <input id="recruitment-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="recruitment-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="recruitment-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
<!--            <button id="recruitment-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>-->
        </div>

        <table class="layui-table" id="recruitment-table" lay-filter="recruitment-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="recruitment-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="recruitment-tpl-state">
    <input type="checkbox" lay-filter="recruitment-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="招聘中|不招聘"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        //if( admin.hasPerm('user:add') ) {
        //   $('#btnAdd').hide();
        //}

        // 添加按钮绑定权限
        if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#recruitment-btn-add').hide();
        }

        // 导出按钮绑定权限
        if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#recruitment-btn-export').hide();
        }



        // 渲染表格
        table.render({
            elem: '#recruitment-table',
            url: config.base_server + 'api-company/recruitment',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '招聘信息编号'},
                {field: 'companyname', sort: true, title: '公司名字'},
                {field: 'skills', sort: true, title: '技术需求'},
                {field: 'employeeCount', sort: true, title: '招聘人数'},
                {field: 'experience', sort: true, title: '工作年限'},
                {field: 'tasks', sort: true, title: '工作职责'},
                {field: 'startTime', sort: true, title: '发布时间'},
                {field: 'endTime', sort: true, title: '截止时间'},

                //
                // {
                //     sort: true, templet: function (d) {
                //         return util.toDateString(d.createTime);
                //     }, title: '创建时间'
                // },
                {field: 'costId', sort: true, title: '费用'},

                {field: 'enabled', sort: true, templet: '#recruitment-tpl-state', title: '状态',width: 150},
                // {field: 'icon', sort: true, title: '公司头像'},
                {align: 'center', toolbar: '#recruitment-table-bar', title: '操作',width: 150}
            ]]
        });

        // 添加按钮点击事件
        $('#recruitment-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(recruitment-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'delete') { // 重置密码
                layer.confirm('确定删除此招聘信息吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-company/company/delre/'+obj.data.id+'', {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            location.reload();
                            layer.closeAll('loading');
                            layer.msg(data.msg, {icon: 1, time: 500});
                        } else {
                            location.reload();
                            layer.closeAll('loading');
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            //修改sex值
            // if (data){
            //     data.sex = data.sex+'';
            // }
            admin.putTempData('t_recruitment', data);
            var title = data ? '修改信息' : '添加信息';
            admin.popupCenter({
                title: title,
                path: 'pages/company/recruitment_form.html',
                finish: function () {
                    table.reload('recruitment-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#recruitment-btn-search').click(function () {
            var key = $('#recruitment-search-key').val();
            var value = $('#recruitment-search-value').val();
            table.reload('recruitment-table', {where: {searchKey: key, searchValue: value}});
        });


        // 导出
        // $('#user-btn-export').click(function () {
        //     var url = config.base_server + 'api-user/users/exportUser?access_token='+config.getToken().access_token;
        //     var xhr = new XMLHttpRequest();
        //     xhr.open('POST', url, true);
        //     xhr.responseType = "blob";
        //     xhr.setRequestHeader("client_type", "DESKTOP_WEB");
        //     xhr.onload = function() {
        //         if (this.status == 200) {
        //             var blob = this.response;
        //             var objecturl = URL.createObjectURL(blob);
        //             window.location.href = objecturl;
        //         }
        //     }
        //     xhr.send();
        // });

        // 修改user状态
        form.on('switch(recruitment-tpl-state)', function (obj) {
            layer.load(2);
            admin.req('api-company/recruitment/updateEnabled', {
                id: obj.elem.value,
                enabled: obj.elem.checked ? true : false
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    //table.reload('table-user', {});
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'GET');
        });
    });
</script>